package com.letwal.cmcvnd.components.path

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.extend.dip2Px


class QRCodeBorderPath : View {

    /**
     * 画笔-路径
     */
    private var mPath: Path = Path()

    /**
     * 画笔-线条
     */
    private var mPaint: Paint = Paint()

    /**
     * 宽度-线条
     */
    private var mStrokeWidth = 4F

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        // 设置画笔
        mPaint.color = ContextCompat.getColor(context!!, R.color.split_line)
        mPaint.isAntiAlias = true
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeCap = Paint.Cap.ROUND
        mPaint.strokeJoin = Paint.Join.ROUND
        mPaint.strokeWidth = context.dip2Px(mStrokeWidth) * 1F
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        //线长度
        val lineHeight = context.dip2Px(mStrokeWidth) / 2F
        val lineWidth = context.dip2Px(16F) * 1F
        val mWidth = width * 1F
        val mHeight = height * 1F

        // 画left top
        val topLeftStartX = lineWidth
        val topLeftStartY = lineHeight
        val topLeftMiddleX = lineHeight
        val topLeftMiddelY = lineHeight
        val topLeftEndX = lineHeight
        val topLeftEndY = lineWidth
        mPath.moveTo(
            topLeftStartX,
            topLeftStartY
        )
        mPath.lineTo(
            topLeftMiddleX,
            topLeftMiddelY,
        )
        mPath.lineTo(
            topLeftEndX,
            topLeftEndY,
        )
        canvas?.drawPath(mPath, mPaint)

        // 画right top
        val topRightStartX = mWidth - lineWidth
        val topRightStartY = lineHeight
        val topRightMiddleX = mWidth - lineHeight
        val topRightMiddelY = lineHeight
        val topRightEndX = mWidth - lineHeight
        val topRightEndY = lineWidth
        mPath.reset()
        mPath.moveTo(
            topRightStartX,
            topRightStartY
        )
        mPath.lineTo(
            topRightMiddleX,
            topRightMiddelY,
        )
        mPath.lineTo(
            topRightEndX,
            topRightEndY,
        )
        canvas?.drawPath(mPath, mPaint)

        // 画left bottom
        val bottomLeftStartX = lineHeight
        val bottomLeftStartY = mHeight - lineWidth
        val bottomLeftMiddleX = lineHeight
        val bottomLeftMiddelY = mHeight - lineHeight
        val bottomLeftEndX = lineWidth
        val bottomLeftEndY = mHeight - lineHeight
        mPath.reset()
        mPath.moveTo(
            bottomLeftStartX,
            bottomLeftStartY
        )
        mPath.lineTo(
            bottomLeftMiddleX,
            bottomLeftMiddelY,
        )
        mPath.lineTo(
            bottomLeftEndX,
            bottomLeftEndY,
        )
        canvas?.drawPath(mPath, mPaint)

        // 画right bottom
        val bottomRightStartX = mWidth - lineHeight
        val bottomRightStartY = mHeight - lineWidth
        val bottomRightMiddleX = mWidth - lineHeight
        val bottomRightMiddelY = mHeight - lineHeight
        val bottomRightEndX = mWidth - lineWidth
        val bottomRightEndY = mHeight - lineHeight
        mPath.reset()
        mPath.moveTo(
            bottomRightStartX,
            bottomRightStartY
        )
        mPath.lineTo(
            bottomRightMiddleX,
            bottomRightMiddelY,
        )
        mPath.lineTo(
            bottomRightEndX,
            bottomRightEndY,
        )
        canvas?.drawPath(mPath, mPaint)
    }
}